function DoubleLink() {
    function Node(element) {
        this.element = element
        this.next = null
        this.prev = null
    }
    this.length = 0
    this.head = null
    this.tail = null
    // 1.在尾部追加新的元素
    DoubleLink.prototype.append = function (element) {
        var newNode = new Node(element)
        if (this.head === null) {
            this.head = newNode
            this.tail = newNode
        } else {
            this.tail.next = newNode
            newNode.prev = this.tail
            this.tail = newNode
        }
        this.length++
    }
    // 2.正向遍历转字符串
    DoubleLink.prototype.fowardToString = function () {
        var current = this.head
        var forwardStr = ''
        while (current) {
            forwardStr = "," + current.element
            current = current.next
            console.log('++++', forwardStr.slice(1));
        }
        return forwardStr.slice(1)
    }
    // 3.反向遍历转字符串
    DoubleLink.prototype.reverseToString = function () {
        var current = this.tail
        var reverseStr = ''
        while (current) {
            reverseStr = "," + current.element
            current = current.prev
            console.log('----', reverseStr.slice(1));
        }
        return reverseStr.slice(1)
    }
    DoubleLink.prototype.toString = function () {
        // console.log('=====', this.fowardToString());
        return this.fowardToString()
    }
    DoubleLink.prototype.insert = function (position, element) {
        if (position < 0 || position > this.length) return false
        var newNode = new Node(element)
        // 在第一个位置插入
        if (position === 0) {
            // 判断链表是否为空
            if (this.head === null) {
                this.head = newNode
                this.tail = newNode
            } else {
                this.head.prev = newNode
                newNode.next = this.head
                this.head = newNode
            }
            // 判断是否在最后进行插入
        } else if (position === this.length) {
            this.tail.next = newNode
            newNode.prev = this.tail
            this.tail = newNode
        } else {
            var index = 0
            var current = this.head
            var previous = null
            while (index++ < position) {
                previous = this.head
                current = current.next
            }
            newNode.next = current
            newNode.prev = previous
            current.prev = newNode
            previous.next = newNode
        }
        this.length++
        return true
    }
    // 根据位置删除
    DoubleLink.prototype.removeAt = function (position) {
        if (position < 0 || position > this.length) return false
        var current = this.head
        if (position === 0) {
            if (this.length == 1) {
                this.head = null
                this.tail = null
            } else {
                this.head.prev = null
                this.head = this.head.next
            }
        } else if (position == this.length - 1) {
            current = this.tail
            this.tail = this.tail.prev
            this.tail.next = null
        } else {
            var index = 0
            var previous = null
            while (index++ < position) {
                previous = current
                current = current.next
            }
            previous.next = current.next
            current.next.prev = previous
        }
        this.length--
        return current.element
    }
    // 根据元素查找
    DoubleLink.prototype.indexOf = function (element) {
        var current = this.head
        var index = 0
        while (current) {
            if (current.element === element) {
                return index
            }
            index++
            current = current.next
        }
        return -1
    }
    // 根据元素删除
    DoubleLink.prototype.remove = function (element) {
        var index = this.indexOf(element)
        return this.removeAt(index)
    }
    // 判断是否为空
    DoublyLinkedList.prototype.isEmpty = function () {
        return this.length === 0
    }
    // 获取链表长度
    DoublyLinkedList.prototype.size = function () {
        return this.length
    }

    // 获取第一个元素
    DoublyLinkedList.prototype.getHead = function () {
        return this.head.element
    }

    // 获取最后一个元素
    DoublyLinkedList.prototype.getTail = function () {
        return this.tail.element
    }
}

var link = new DoubleLink()
// link.append(1)
// link.append(2)
// link.append(3)
link.insert(0, '100')
link.insert(1, '200')
link.insert(2, '300')
link.insert(1, '400')
console.log(link.removeAt(0));
link.removeAt(2)
// console.log(link);
// link.fowardToString()
// link.reverseToString()
// link.toString()

